home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9667 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: presby.edu!jtbell
  3. From: jtbell@presby.edu (Jon Bell)
  4. Subject: Re: Class
  5. Message-ID: <DnpEHt.BH3@presby.edu>
  6. Date: Sun, 3 Mar 1996 18:10:40 GMT
  7. References: <4hchoh$ngh@nova.umuc.edu>
  8. Organization: Presbyterian College, Clinton, South Carolina USA
  9.  
  10. [posted and e-mailed]
  11.  
  12.  Steve Russell <srussell@nova.umuc.edu> wrote:
  13. >1. Can I read from and output to files directly from the class?  Can
  14. >   I include<fstream.h> in the class?  Do I need to include other things
  15. >   like iostream.h and other various libraries for library functions?
  16.  
  17. Yes, you can #include header files like iostream.h, fstream.h, math.h, 
  18. etc.  In fact, you *must* #include them in the header file for your 
  19. class, if you use any of that stuff in your class, and you want it to 
  20. compile.
  21.  
  22. >3. Is this the right way to right a file for a class in unix:
  23. >
  24. >    //FILE: EmpRecords.h
  25. >       // MANIPULATES EMPLOYEE RECORDS
  26. >       #ifdef EMPLOYEERECORDS_H_
  27. >       #define EMPLOYEERECORDS_H_
  28. >
  29. >    // class definition, and member function definitions
  30. >                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- NO!
  31. >    #endif//end of EmpRecords.h
  32.  
  33. EmpRecords.h normally contains only the class definition, which in turn 
  34. contains prototypes for the member functions.  The member functions are 
  35. normally defined in a separate file, EmpRecords.cc or EmpRecords.cp, 
  36. which #includes EmpRecords.h, and which you usually compile separately 
  37. from the main program.  On our system, using g++, it might go something 
  38. like this:
  39.  
  40.     g++ EmpRecords.cc -c       [which produces EmpRecords.o]
  41.     g++ MyProg.cc EmpRecords.o -lm -o MyProg
  42.           [hypothetical situation in which you need the math library, -lm]
  43.  
  44. This way you need to recompile EmpRecords only if you change it.
  45.  
  46. >4.  Finally, my class has a struct in it.  This struct has a char array in it.
  47. >    Do I need to initialize this struct via a constructor function?  If so
  48. >    would it be sufficent to set all the attributes of the struct to zero
  49. >    and give a dummy name to the string?
  50.  
  51. If your class doesn't need to allocate memory with 'new', and you're 
  52. happy with letting everything be initialized to zero, you can probably 
  53. get away without a constructor.  Nevertheless, it's nice to have one, 
  54. just in case you ever want to specify initial values for the member data 
  55. when you declare an object of that type, e.g.:
  56.  
  57.     EmployeeRecord
  58.         NewHire(LastName, FirstName, Salary, ...);
  59.  
  60. -- 
  61. Jon Bell <jtbell@presby.edu>                        Presbyterian College
  62. Dept. of Physics and Computer Science        Clinton, South Carolina USA
  63.